exports.main = async (context) => {
    const param = context.data
    if (!param.dbOp || !param.collectionName) {
        throw 'incorrect parameter'
    }

    const cloud = context.cloud
    if (param.dbOp == 'create') {
        return await cloud.db.createCollection(param.collectionName)
    }
    const collection = cloud.db.collection(param.collectionName)
    switch (param.dbOp) {
        case 'find': {
            return await collection.find(param.filter, param.operation)
        }
        case 'count': {
            return await collection.count(param.filter)
        }
        case 'insertOne': {
            return await collection.insertOne(param.data)
        }
        case 'insertMany': {
            return await collection.insertMany(param.data)
        }
        case 'deleteMany': {
            return await collection.deleteMany(param.filter)
        }
        case 'replaceOne': {
            return await collection.replaceOne(param.filter, param.data)
        }
        case 'updateMany': {
            return await collection.updateMany(param.filter, param.data, param.arrayFilters)
        }
        case 'aggregate': {
            return await collection.aggregate(param.pipeline)
        }
        default:
            throw 'incorrect dbOp:' + param.dbOp
    }
};